# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from sqlalchemy import func
from app.modules.common.dbUtil import createId

"""
操作记录
"""


class OperationRecord(db.Model, JsonEncoder):
    __tablename__ = "operation_record"
    id = db.Column(db.String, primary_key = True)
    create_date = db.Column(db.DateTime, default = func.now())
    user_id = db.Column(db.String)
    user_name = db.Column(db.String)
    operation_type = db.Column(db.String)
    desc = db.Column(db.String)
    key = db.Column(db.String)
    old_value = db.Column(db.String)
    new_value = db.Column(db.String)

    def __init__(self, user_id, user_name, operation_type, desc, key, old_value, new_value):
        self.id = createId("op")
        self.user_id = user_id
        self.user_name = user_name
        self.operation_type = operation_type
        self.desc = desc
        self.key = key
        self.old_value = old_value
        self.new_value = new_value

    def to_dict(self):
        return {
            'id': self.id,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'user_id': self.user_id,
            'user_name': self.user_name,
            'operation_type': self.operation_type,
            'desc': self.desc,
            'key': self.key,
            'old_value': self.old_value,
            'new_value': self.new_value,
        }
